#include "MnistArray.hpp"
const int totalElements = 28 * 28; // 二维数组的总元素数量
int16_t mnistData[totalElements]; // 一维数组存储接收到的数据

void testSerialInput(int16_t* dataArray, int* dataCount) {
  static int16_t localDataArray[1000];  // 用于接收数据的本地数组
  static int localDataCount = 0;          // 用于记录已接收的数据个数

  if (Serial.available() > 0) {
    String inputData = Serial.readString();
    // Serial.print("Received data: ");
    // Serial.println(inputData);

    int commaIndex = inputData.indexOf(',');
    while (commaIndex != -1) {
      String numberString = inputData.substring(0, commaIndex);
      if (numberString.length() > 0) {
        int16_t number = numberString.toInt();
        if (localDataCount < sizeof(localDataArray) / sizeof(localDataArray[0])) {
          localDataArray[localDataCount++] = number;
        }
      }
      inputData = inputData.substring(commaIndex + 1);
      commaIndex = inputData.indexOf(',');
    }
    // 处理最后一个数据（如果有）
    if (inputData.length() > 0) {
      int16_t number = inputData.toInt();
      if (localDataCount < sizeof(localDataArray) / sizeof(localDataArray[0])) {
        localDataArray[localDataCount++] = number;
      }
    }

    // 打印接收的数据个数
    Serial.print("Array size: ");
    Serial.println(localDataCount);

    // 打印数组内容
    for (int i = 0; i < localDataCount; i++) {
      Serial.print(localDataArray[i]);
      Serial.print(" ");
    }
    Serial.println();
    if(localDataCount <= 784)
    {
      // 将本地数据复制到传入的参数中
      for (int i = 0; i < localDataCount; i++) {
        dataArray[i] = localDataArray[i];
      }
      *dataCount = localDataCount;  // 设置接收的数据个数
    }
    // 清空串口接收缓存，准备下一次接收
    Serial.flush(); // 清空发送缓存
    // 重置本地数据计数器
    localDataCount = 0;
  }
  else
  {
    *dataCount = localDataCount; 
  }
}








